package com.aaa.stream02;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2022/11/6
 */
public class AllTest {
    @Test
    public void test1(){
        String[] arr1={"a","b","c","d"};
        String[] arr2={"d","e","f","g"};
        Stream<String> stream1=Stream.of(arr1);
        Stream<String> stream2=Stream.of(arr2);

        //合并流
        Stream.concat(stream1,stream2).forEach(System.out::println);
        //去重
        Stream.concat(stream1,stream2).distinct().forEach(System.out::println);
        //获取前n个元素
        Stream.concat(stream1,stream2).limit(5).forEach(System.out::println);
        //跳过前n个元素
        Stream.concat(stream1,stream2).skip(3).forEach(System.out::println);
    }
    @Test
    public void Test2(){
        Integer[] arr={4,1,7,2,49,3};
        Optional<Integer> reduce = Arrays.stream(arr).reduce((t1, t2) -> t1 += t2);
        System.out.println("reduce = " + reduce);
    }
    @Test
    public void Test3(){
        ArrayList<Person> people = new ArrayList<>();
        people.add(new Person("上官雪", 18, "中国", 'A'));
        people.add(new Person("慕容复", 14, "法国", 'B'));
        people.add(new Person("逍遥散", 57, "伊拉克", 'B'));
        people.add(new Person("宇文都", 36, "比利时", 'B'));
        people.add(new Person("易烊玺", 45, "中国", 'A'));
        people.add(new Person("无为治", 15, "荷兰", 'A'));

        //1.找到年龄大于18岁的人并输出
//            people.stream().filter(item->item.getAge()>18).forEach(System.out::println);
        //2.找出所有中国人的数量
//            people.stream().filter(item->item.getCountry().equals("中国")).forEach(System.out::println);
        //3.map映射操作--名称
//            people.stream().map(item->item.getName()).forEach(System.out::println);
        //4.sort排序操作--年龄
//        people.stream().sorted((o1, o2) ->Integer.compare(o1.getAge(),o2.getAge())).forEach(System.out::println);
    }
    @Test
    public void Test4(){
        ArrayList<Person> people = new ArrayList<>();
        people.add(new Person("上官雪", 18, "中国", 'A'));
        people.add(new Person("慕容复", 14, "法国", 'B'));
        people.add(new Person("逍遥散", 57, "伊拉克", 'B'));
        people.add(new Person("宇文都", 36, "比利时", 'B'));
        people.add(new Person("易烊玺", 45, "中国", 'A'));
        people.add(new Person("无为治", 15, "荷兰", 'A'));
        //find操作--findFirst和findAny
        //findFirst过去流中第一个元素
            /*Optional<Person> first = people.stream().findFirst();
            System.out.println("first = " + first);*/
        //match匹配操作--allMatch anyMatch
        //所有元素都必须匹配条件 才返回true
           /* Optional<Person> any = people.stream().findAny();
            Optional<Person> any1 = people.parallelStream().findAny();
            System.out.println("any1 = " + any1);
            System.out.println("any = " + any);*/
        //聚合操作max min count
        //总的个数count
            /*long count = people.stream().count();
            System.out.println("count = " + count);*/
        //max
            /*Optional<Person> max = people.stream().max(((o1, o2) -> o1.getAge() - o2.getAge()));
            System.out.println("max = " + max);*/
            /*Optional<Person> min = people.stream().min(((o1, o2) -> o1.getAge() - o2.getAge()));
            System.out.println("min = " + min);*/
    }
    @Test
    public void Test5(){
        ArrayList<Person> people = new ArrayList<>();
        people.add(new Person("上官雪", 18, "中国", 'A'));
        people.add(new Person("慕容复", 14, "法国", 'B'));
        people.add(new Person("逍遥散", 57, "伊拉克", 'B'));
        people.add(new Person("宇文都", 36, "比利时", 'B'));
        people.add(new Person("易烊玺", 45, "中国", 'A'));
        people.add(new Person("无为治", 15, "荷兰", 'A'));
        //collect--搜集操作
        /*计数： count
        平均值： averagingInt、 averagingLong、 averagingDouble
        最值： maxBy、 minBy
        求和： summingInt、 summingLong、 summingDouble*/
        Long collect = people.stream().collect(Collectors.counting());
        System.out.println("计数" + collect);
        Double collect1 = people.stream().collect(Collectors.averagingInt(item -> item.getAge()));
        System.out.println("平均值年龄 = " + collect1);
        Optional<Person> collect2 = people.stream().collect(Collectors.maxBy(((o1, o2) -> o1.getAge() - o2.getAge())));
        System.out.println("最大年龄 = " + collect2);
        Optional<Person> collect3 = people.stream().collect(Collectors.minBy((o1, o2) -> o1.getAge() - o2.getAge()));
        System.out.println("最小年龄 = " + collect3);
        //搜集为一个新的集合
        //获取过滤后的筛选后的指定数据
        Set<String> collect4 = people.stream().filter(item -> item.getAge() > 20)
                .filter(item -> item.getCountry().equals("中国"))
                .map(item -> item.getName())
                .collect(Collectors.toSet());
        System.out.println("根据年龄和国籍输出对应的姓名 = " + collect4);
        //分组操作
        //根据性别分组
        Map<Character, List<Person>> collect5 = people.stream().collect(Collectors.groupingBy(item -> item.getSex()));
        System.out.println("根据性别分组后的结果 = " + collect5);
    }
    @Test
    public void Test6(){
        //时间格式化可能出现安全问题
        //自定义时间的日期--设计不合理
        Date date = new Date(2022,11,6);
        System.out.println("date = " + date);
        //时间格式化---存在线程安全问题
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("format = " + format);
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(format.parse("2022-11-6"));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}


@Data
@NoArgsConstructor
@AllArgsConstructor
class Person{
    private String name;
    private Integer age;
    private String country;
    private char sex;
}